﻿using System.Drawing;

namespace MDAControlsFramework.Helpers {
	/// <summary>
	/// Trida pro uchovani vlastnosti textu
	/// </summary>
	public class MDATextStyle: IMDAObject {
		static Color cColor = Color.Black;
		const string cFontName = "Arial";
		const float cFontSize = 10;
		static FontStyle cFontStyle = FontStyle.Bold;

		private Color _color;
		private string _colorName = "";
		private bool _wordWrap = true;
		private string _fontName;
		private float _fontSize;
		private FontStyle _fontStyle;

		// predpocitane hodnoty
		public Font _font = new Font(cFontName, cFontSize, cFontStyle);
		public Brush _brush = new SolidBrush(cColor);
		public StringFormat _format = new StringFormat();

		public static MDATextStyle None;

		public MDATextStyle() {
			_fontName = cFontName;
			_fontSize = cFontSize;
			_fontStyle = cFontStyle;
			_color = cColor;
			_brush = new SolidBrush(_color);
			_font = new Font(_fontName, _fontSize, _fontStyle);
		}

		public MDATextStyle(string fontName, float fontSize, FontStyle fontStyle, Color color) {
			_fontName = fontName;
			_fontSize = fontSize;
			_fontStyle = fontStyle;
			_color = color;
			_brush = new SolidBrush(color);
			_font = new Font(_fontName, _fontSize, _fontStyle);
		}

		public MDATextStyle(Font font, Color color, bool wordWrap) {
			_font = font;
			_color = color;
			_wordWrap = wordWrap;
			_brush = new SolidBrush(_color);
			if (wordWrap) {
				_format.FormatFlags = StringFormatFlags.NoClip;
			} else {
				_format.FormatFlags = StringFormatFlags.NoWrap;
			}
		}

		public object Clone() {
			return new MDATextStyle((Font)_font.Clone(), _color, _wordWrap);
		}

		/// <summary>
		/// nastaví vlastnosti podle objektu v parametru
		/// </summary>
		/// <param name="textStyle">objekt, podle kterého se vlastnosti nastavují</param>
		public void Update(MDATextStyle textStyle) {
			if (textStyle == null)
				return;

			if (textStyle.Color != cColor)
				this.Color = textStyle.Color;

			if (textStyle._fontName != cFontName)
				this.FontName = textStyle.FontName;

			if (textStyle._fontSize != cFontSize)
				this.FontSize = textStyle.FontSize;

			if (textStyle.FontStyle != cFontStyle)
				this.FontStyle = textStyle.FontStyle;
		}

		#region property

		public Font Font {
			get {
				return _font;
			}
		}

		public Brush Brush {
			get {
				return _brush;
			}
		}

		public StringFormat Format {
			get {
				return _format;
			}
		}

		public bool WordWrap {
			get {
				return _wordWrap;
			}
			set {
				_wordWrap = value;
				if (value) {
					_format.FormatFlags = StringFormatFlags.NoClip;
				} else {
					_format.FormatFlags = StringFormatFlags.NoWrap;
				}
			}
		}

		public float FontSize {
			get {
				return _fontSize;
			}
			set {
				_fontSize = value;
				_font = new Font(_fontName, _fontSize, _fontStyle);
			}
		}

		public string FontName {
			get {
				return _fontName;
			}
			set {
				_fontName = value;
				_font = new Font(_fontName, _fontSize, _fontStyle);
			}
		}

		public FontStyle FontStyle {
			get {
				return _fontStyle;
			}
			set {
				_fontStyle = value;
				_font = new Font(_fontName, _fontSize, _fontStyle);
			}
		}

		public Color Color {
			get {
				return _color;
			}
			set {
				_color = value;
				_brush = new SolidBrush(value);
			}
		}

		public string GetName() {
			return "null";
		}

		#endregion

		#region IMDAObject Members

		public bool AddObject(IMDAObject objectInstance) {
			return false;
		}

		/// <summary>
		/// Set propetry by name
		/// </summary>
		/// <param name="name">name of property in lowercase</param>
		/// <param name="value">value of property in string</param>
		/// <returns>if property was set</returns>
		public bool SetProperty(string name, string value) {
			switch (name) {
				case "font":
					this.FontName = value;
					return true;
				case "size":
					this.FontSize = Parser.parseFloat(value);
					return true;
				case "fontstyle":
					object newFontStyle = Parser.parseEnum(value, typeof(FontStyle));
					if (newFontStyle is FontStyle) {
						this.FontStyle = (FontStyle)newFontStyle;
						return true;
					} else {
						return false;
					}
				case "color":
					this.Color = Parser.parseColor(value);
					_colorName = value;
					return true;
				case "align":
					_format.Alignment = Parser.parseStringFormat(value, false);
					_format.LineAlignment = Parser.parseStringFormat(value, true);
					return true;
				default:
					return false;
			}
		}

		public void AddEvent(string eventName, string eventHandler) {
			return;
		}

		#endregion

		public override string ToString() {
			return "{TStyle: " + _fontName + " " + _fontSize + " " + _colorName + " " + _wordWrap + "}";
		}
	}
}